package com.smsp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smsp.common.Const;
import com.smsp.common.ServerResponse;
import com.smsp.dao.UserMapper;


import com.smsp.entity.User;
import com.smsp.service.UserService;
import com.smsp.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

/**
 * @author DWJ
 */
@Service("UserService")
@Transactional
public class UserServiceImpl implements UserService {

    public Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;


    @Override
    public ServerResponse addUser(User user) {

        if(user.getUsername()==null){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        if(userMapper.selectUserByUsername(user.getUsername())!=null){
            return ServerResponse.createByErrorMessage("用户名已存在");
        }
        user.setPassword(MD5Util.MD5EncodeUtf8(Const.DEFAULT_PASSWORD));
        user.setUserStatus("0");
        user.setCreateTime(new Date());
        if(userMapper.insertSelective(user)>0){
            logger.info("用户插入成功"+user);
            return ServerResponse.cerateBySuccessMessage("用户添加成功");
        }
        logger.debug("用户插入失败"+user);
        return ServerResponse.createByErrorMessage("用户添加失败");
    }


    @Override
    public ServerResponse updateUser(User user) {
        user.setUpdateTime(new Date());
        if(userMapper.updateByPrimaryKeySelective(user)>0){
            logger.info("用户记录修改成功"+user);
            return ServerResponse.cerateBySuccessMessage("操作成功");
        }
        logger.debug("用户记录修改失败"+user);
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @Override
    public ServerResponse selectAll() {
        List<User> userList = userMapper.selectAll();
        return ServerResponse.cerateBySuccess("用户信息查询成功",userList);
    }

    @Override
    public ServerResponse delUser(int id) {

        if(userMapper.deleteByPrimaryKey(id)>0){
            logger.info("删除记录id为"+id);
            return ServerResponse.cerateBySuccessMessage("用户删除成功");
        }
        logger.debug("删除记录失败");
        return ServerResponse.createByErrorMessage("用户删除失败");
    }

    @Override
    public ServerResponse login(String username, String password) {
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectByUsernamePassword(username,md5Password);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(Const.ERROR_PASSWORD_NOT_TRUE,"密码错误");
        }
        if(user.getUserStatus()!=null && !Const.STATUS_DEFAULT.equals(user.getUserStatus())){
            return ServerResponse.createByErrorMessage("账号被冻结");
        }
        return ServerResponse.cerateBySuccess("登录成功",user);
    }

    @Override
    public PageInfo<User> findMessageDetailByPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<User> list = userMapper.selectAll();
        return new PageInfo<User>(list);
    }

    @Override
    public ServerResponse updatePwd(User user, String newPwd){
        if(userMapper.selectByUsernamePassword(user.getUsername(),user.getPassword())==null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        user.setPassword(MD5Util.MD5EncodeUtf8(newPwd));
        user.setUpdateTime(new Date());
        if(userMapper.updateByPrimaryKeySelective(user)>0){
            return ServerResponse.cerateBySuccessMessage("密码修改成功");
        }
        return ServerResponse.createByErrorMessage("密码修改失败");
    }
}
